package com.mall.common.controller;

import java.security.interfaces.RSAPublicKey;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.common.service.AreaService;
import com.mall.common.service.FileService;
import com.mall.common.service.MyCaptchaService;
import com.mall.common.service.RSAService;
import com.mall.common.util.ResponseUtils;

/**
 * 公共action类
 */
@Controller("frontCommon")
@RequestMapping("/common")
public class CommonController {

	@Resource private MyCaptchaService myCaptchaService;
	@Resource private RSAService rSAService;
	@Resource private AreaService areaService;
	@Resource private FileService fileService;
	
	/**
	 * 资源未找到页面
	 * @return
	 */
	@RequestMapping(value="/resource_not_found", method=RequestMethod.GET)
	public String resourceNotFound() {
		return "/resource_not_found";
	}
	
	/**
	 * 加密
	 * @param request
	 * @return
	 */
	@RequestMapping("/publicKey")
	@ResponseBody
	public JSONObject publicKey(HttpServletRequest request){
		RSAPublicKey rsapublickey = rSAService.generateKey(request, false);
		JSONObject obj = new JSONObject();
		obj.put("modulus", Base64.encodeBase64String(rsapublickey.getModulus().toByteArray()));
		obj.put("exponent", Base64.encodeBase64String(rsapublickey.getPublicExponent().toByteArray()));
		return obj;
	}
	
	/**
	 * 获取验证码
	 * @param captchaId
	 * @param request
	 * @param response
	 */
	@RequestMapping("/captcha")
	public void captcha(String captchaId, HttpServletRequest request, HttpServletResponse response) {
		if (StringUtils.isEmpty(captchaId)){
			captchaId = request.getSession().getId();
		}
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Cache-Control", "no-store");
		response.setDateHeader("Expires", 0L);
		response.setContentType("image/jpeg");
		ServletOutputStream servletoutputstream = null;
		try {
			servletoutputstream = response.getOutputStream();
			java.awt.image.BufferedImage bufferedimage = myCaptchaService.buildImage(captchaId);
			ImageIO.write(bufferedimage, "jpg", servletoutputstream);
			servletoutputstream.flush();
		} catch (Exception exception) {
			exception.printStackTrace();
		} finally {
			IOUtils.closeQuietly(servletoutputstream);
		}
	}
	
	/**
	 * 地区级联
	 * @param path
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/area", method=RequestMethod.GET)
	public String area(Long path, HttpServletResponse response) {
		return ResponseUtils.renderJSON(areaService.getAreaByPath(path), response);
	}

}